﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SC2Ranks
{
    internal static class Helper
    {
        public static Region ParseRegion(string value)
        {
            return regions[value];
        }

        public static string ConvertRegion (Region region)
        {
            return Enum.GetName(typeof(Region), region).ToLower();
        }

        public static Race ParseRace(string value)
        {
            return races[value];
        }

        public static string ConvertRace(Race race)
        {
            return Enum.GetName(typeof(Race), race).ToLower();
        }

        public static League ParseLeague(string value)
        {
            return leagues[value];
        }

        public static string ConvertLeague(League league)
        {
            return Enum.GetName(typeof(League), league).ToLower();
        }

        static Dictionary<string, Region> regions = new Dictionary<string, Region>
        {
            { "eu", Region.EU },
            { "kr", Region.KR },
            { "la", Region.LA },
            { "sea", Region.SEA },
            { "tw", Region.TW },
            { "us", Region.US }
        };

        static Dictionary<string, Race> races = new Dictionary<string, Race> 
        { 
            { "protoss", Race.Protoss },
            { "terran", Race.Terran },
            { "zerg" , Race.Zerg },
            { "random", Race.Random }
        };

        static Dictionary<string, League> leagues = new Dictionary<string, League> 
        {
            { "bronze", League.Bronze },
            { "silver", League.Silver },
            { "gold", League.Gold },
            { "platinum", League.Platinum },
            { "diamond", League.Diamond }
        };

        public static string GetSearchRequestBody(IEnumerable<PlayerSearchInfo> players)
        {
            List<string> values = new List<string>();

            int i = 0;
            foreach (var player in players)
            {
                values.Add(string.Format("characters[{0}][region]={1}", i, Helper.ConvertRegion(player.Region)));
                values.Add(string.Format("characters[{0}][name]={1}", i, player.Name));
                if (player.CharacterCode.HasValue)
                    values.Add(string.Format("characters[{0}][code]={1}", i, player.CharacterCode.Value));
                else if (player.BattleNetID.HasValue)
                    values.Add(string.Format("characters[{0}][bnet_id]={1}", i, player.BattleNetID.Value));
                else
                    throw new ArgumentNullException("CharacterCode or BattleNetID must have a value");

                i = i + 1;
            }
            return string.Join("&", values.ToArray());
        }
    }
}
